package com.jaiky.imagespickers.container;

import com.jaiky.imagespickers.*;
import com.jaiky.imagespickers.preview.MultiImgShowAbility;
import com.jaiky.imagespickers.preview.ViewHolder;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;


public class SimpleImageAdapter extends BaseItemProvider {
    private boolean isDelete;
    private final ImagePicker imagePicker;
    private StackLayout.LayoutConfig fl = null;
    private int containerWidth = 1080;
    private int rowCount;
    private int size;
    private List<ImageItem> imageItems = new ArrayList<>();
    private Context mContext;

    private void initImgSize() {
        // 带删除
        if (isDelete) {
            // 如果行数为4，中间间隔3个3dp共9dp + 距离右边4个8dp共32dp
            size = containerWidth - AttrHelper.vp2px(3 * (rowCount - 1) + 8 * rowCount, mContext);
            size = Math.round(size / (float) rowCount);
            fl = new StackLayout.LayoutConfig(size, size);
            //填充上右，为删除按钮让出空间
            fl.setMargins(0, AttrHelper.vp2px(8, mContext), AttrHelper.vp2px(8, mContext), 0);
        }
        // 不带删除
        else {
            // 如果行数为4，中间间隔3个10dp共30dp
            size = containerWidth - AttrHelper.vp2px(10 * (rowCount - 1), mContext);
            size = Math.round(size / (float) rowCount);
            fl = new StackLayout.LayoutConfig(size, size);
        }
    }

    public SimpleImageAdapter(Context ability, boolean isDelete, int rowCount) {
        this.mContext = ability;
        this.containerWidth = DisplayManager.getInstance().getDefaultDisplay(mContext).get().getAttributes().width - 20;
        this.isDelete = isDelete;
        this.rowCount = rowCount;
        imagePicker = ImagePicker.getInstance();
        initImgSize();
    }

    // 更新数据
    public void refreshData(List<ImageItem> datas) {
        if (datas == null || datas.size() == 0) {
            this.imageItems = new ArrayList<>();
        } else {
            this.imageItems = datas;
        }
        notifyDataChanged();
    }

    @Override
    public int getCount() {
        return imageItems.size();
    }

    @Override
    public Object getItem(int position) {
        return imageItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder;
        if (component == null) {
            viewHolder = new ImageViewHolder(LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_ability_gradview_item, componentContainer, false));
        } else {
            viewHolder = (ViewHolder) component.getTag();
        }
        ((ImageViewHolder) viewHolder).bind(position);
        return viewHolder.getRootView();
    }

    private class ImageViewHolder extends ViewHolder {
        Component rootView;
        Image ivImage;
        Image ivDelete;

        ImageViewHolder(Component itemView) {
            rootView = itemView;
            ivImage = (Image) itemView.findComponentById(ResourceTable.Id_ability_item_ivImage);
            ivDelete = (Image) itemView.findComponentById(ResourceTable.Id_ability_item_ivDelete);
            itemView.setLayoutConfig(fl); //让图片是个正方形
            itemView.setTag(this);
        }

        void bind(final int position) {
            final ImageItem imageItem = (ImageItem) getItem(position);
            imagePicker.getImageLoader().displayImage((Ability) mContext, imageItem.uriSchema, ivImage, size, size);
            ivImage.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    Intent intent = new Intent();
                    Operation operation = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName(mContext.getBundleName())
                            .withAbilityName(MultiImgShowAbility.class.getName())
                            .build();

                    DataHolder.getInstance().save(DataHolder.DH_CURRENT_IMAGE_FOLDER_ITEMS, imageItems);
                    intent.setOperation(operation);
                    intent.setParam(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
                    intent.setParam("preview", "preview");
                    ((Ability) mContext).startAbility(intent);
                }
            });

            // 是否带删除
            if (isDelete) {
                ivDelete.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        //移除图片
                        imageItems.remove(imageItem);
                        notifyDataChanged();
                    }
                });
            }
        }

        @Override
        public Component getRootView() {
            return rootView;
        }
    }
}
